﻿using Jacobi.Vst.Framework;
using Levfier.FileIO;
using Levfier.GUI.Status;
using Levfier.GUI;
using System.Collections.Generic;
using System.IO;

namespace Levfier
{
    /// <summary>
    /// This class manages custom persistence for all the plugin's programs and parameters.
    /// </summary>
    internal class PluginPersistence : IVstPluginPersistence //VstPluginPersistenceBase
    {
        private FxPluginRoot _plugin;

        /// <summary>
        /// Constructs a new instance.
        /// </summary>
        /// <param name="plugin">Must not be null.</param>
        public PluginPersistence(FxPluginRoot plugin)
        {
            _plugin = plugin;
        }

        //protected override VstProgramReaderBase CreateProgramReader(Stream input)
        //{
        //    return new VstProgramReader(_plugin, input, Encoding);
        //}

        //private class VstProgramReader : VstProgramReaderBase
        //{
        //    private FxPluginRoot _plugin;

        //    public VstProgramReader(FxPluginRoot plugin, Stream input, Encoding encoding)
        //        : base(input, encoding)
        //    {
        //        _plugin = plugin;
        //        //AmplifierFile file = new AmplifierFile();
        //        //IEnumerable<ControlStatus> statuses = file.ReadFromStream(input);
        //        //WpfControlWrapper<MainWindow>.Instance.InitStatuses(statuses);
        //    }

        //    protected override VstProgram CreateProgram()
        //    {
        //        VstProgram program = new VstProgram(_plugin.ParameterFactory.Categories);

        //        _plugin.ParameterFactory.CreateParameters(program.Parameters);

        //        return program;
        //    }
        //}

        public bool CanLoadChunk(Jacobi.Vst.Core.VstPatchChunkInfo chunkInfo)
        {
            return true;
        }

        /// <summary>
        /// 状態を復元する
        /// </summary>
        /// <param name="stream">ストリーム</param>
        /// <param name="programs">謎</param>
        public void ReadPrograms(Stream stream, VstProgramCollection programs)
        {
            AmplifierFile file = new AmplifierFile();
            IEnumerable<ControlStatus> statuses = file.ReadFromStream(stream);
            _plugin.Window.InitStatuses(statuses);
        }

        /// <summary>
        /// 状態を保存する
        /// </summary>
        /// <param name="stream">ストリーム</param>
        /// <param name="programs">謎</param>
        public void WritePrograms(Stream stream, VstProgramCollection programs)
        {
            //base.WritePrograms(stream, programs);
            AmplifierFile file = new AmplifierFile();
            file.WriteToStream(stream, _plugin.Window.GetStatusCreaters());
        }
    }
}
